package com.wanjia.estate.utils;


import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class LinuxUtil {
    public static void
    LinuxPdf(String cmd){
        log.debug(cmd);
        Process p;
        try
        {
            //执行命令
            p = Runtime.getRuntime().exec(cmd);
            //取得命令结果的输出流
            InputStream fis=p.getInputStream();
            //用一个读输出流类去读
            InputStreamReader isr=new InputStreamReader(fis);
            //用缓冲器读行
            BufferedReader br=new BufferedReader(isr);
            String line=null;
            //直到读完为止
            while((line=br.readLine())!=null)
            {
                System.out.println(line);
                log.info("转pdf{}：",line);
            }

            if (cmd.trim().startsWith("docker")) {
                String[] cmdList = cmd.split("\\s+");
                String[] roots = cmdList[8].split(":");
                String target = cmdList[16];
                File targetFile = new File(target.replace(roots[1], roots[0]));
                log.debug("target pef file: {}", targetFile.getAbsolutePath());
                boolean success = false;
                for (int i = 0; i < 30; i++) {
                    if (targetFile.exists()) {
                        success = true;
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException("线程等待异常", e);
                    }
                }

                if (!success) {
                    throw new RuntimeException("文件生成超时");
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
